package com.bj.zspace;

import com.bjzspace.proto.HelloProto;
import com.bjzspace.proto.HelloServiceGrpc;
import com.google.common.collect.Lists;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.stub.StreamObserver;
import org.junit.Test;

import java.io.IOException;
import java.util.Iterator;

public class GrpcClient1 {

    //public static GrpcTracing grpcTracing = ZipkinBraveTracing.create("rpc-grpc-client");

    @Test
    public void testHello() {

        //1.创建通信的管道
        ManagedChannel managedChannel = ManagedChannelBuilder.forAddress("127.0.0.1", 9999).usePlaintext().build();
        try {
            //Channel interceptChannel = ClientInterceptors.intercept(managedChannel, grpcTracing.newClientInterceptor());

            //2.获得代理对象 stub
            // 阻塞的方式
            HelloServiceGrpc.HelloServiceBlockingStub helloService = HelloServiceGrpc.newBlockingStub(managedChannel);
            //3.完成Rpc调用
            //3.1准备参数
            HelloProto.HelloRequest.Builder builder = HelloProto.HelloRequest.newBuilder();
            builder.setName("lwq");
            HelloProto.HelloRequest helloRequest = builder.build();
            //3.2完成调用,获取响应的内容

            HelloProto.HelloResponse hello = helloService.hello(helloRequest);
            String result = hello.getResult();
            System.out.println("result = " + result);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            managedChannel.shutdown();
        }

    }

    @Test
    public void testHello1() {
        ManagedChannel managedChannel = ManagedChannelBuilder.forAddress("localhost", 9999).usePlaintext().build();
        //Channel interceptChannel = ClientInterceptors.intercept(managedChannel, grpcTracing.newClientInterceptor());
        try {
            HelloServiceGrpc.HelloServiceBlockingStub helloService =
                    HelloServiceGrpc.newBlockingStub(managedChannel);
            HelloProto.HelloRequest1.Builder builder = HelloProto.HelloRequest1.newBuilder();
            builder.addAllName(Lists.newArrayList("xiao qiao", "xi shi"));
            HelloProto.HelloRequest1 helloRequest1 = builder.build();
            HelloProto.HelloResponse1 helloResponse1 = helloService.hello1(helloRequest1);
            String result = helloResponse1.getResult();
            System.out.println("hello1 result:" + result);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            managedChannel.shutdown();
        }
    }

    @Test
    public void testC2ss() {

        ManagedChannel managedChannel = ManagedChannelBuilder.forAddress("localhost", 9999).usePlaintext().build();
        //Channel interceptChannel = ClientInterceptors.intercept(managedChannel, grpcTracing.newClientInterceptor());
        try {
            //阻塞的方式
            HelloServiceGrpc.HelloServiceBlockingStub helloService =
                    HelloServiceGrpc.newBlockingStub(managedChannel);
            HelloProto.HelloRequest.Builder builder = HelloProto.HelloRequest.newBuilder();
            builder.setName("kai");
            HelloProto.HelloRequest request = builder.build();
            Iterator<HelloProto.HelloResponse> helloResponseIterator = helloService.c2ss(request);
            while (helloResponseIterator.hasNext()) {
                System.out.println(helloResponseIterator.next().getResult());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            managedChannel.shutdown();
        }
    }


    @Test
    public void testC2ssAsync() {

        ManagedChannel managedChannel = ManagedChannelBuilder.forAddress("localhost", 9999).usePlaintext().build();
        //Channel interceptChannel = ClientInterceptors.intercept(managedChannel, grpcTracing.newClientInterceptor());
        try {
            // 异步非阻塞
            HelloServiceGrpc.HelloServiceStub helloServiceStub = HelloServiceGrpc.newStub(managedChannel);
            HelloProto.HelloRequest.Builder builder = HelloProto.HelloRequest.newBuilder();
            builder.setName("hua mu lan");
            HelloProto.HelloRequest request = builder.build();
            helloServiceStub.c2ss(request, new StreamObserver<HelloProto.HelloResponse>() {
                @Override
                public void onNext(HelloProto.HelloResponse value) {
                    System.out.println("listener:" + value.getResult());
                }

                @Override
                public void onError(Throwable t) {
                    System.out.println("listener error :" + t.getMessage());
                }

                @Override
                public void onCompleted() {
                    System.out.println("listener completed");
                }
            });

            System.out.println("send success");

            System.in.read();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            managedChannel.shutdown();
        }
    }

    @Test
    public void testCs2s() throws IOException {
        ManagedChannel managedChannel = ManagedChannelBuilder.forAddress("127.0.0.1", 9999).usePlaintext().build();
        //Channel interceptChannel = ClientInterceptors.intercept(managedChannel, grpcTracing.newClientInterceptor());
        HelloServiceGrpc.HelloServiceStub helloService = HelloServiceGrpc.newStub(managedChannel);
        StreamObserver<HelloProto.HelloRequest> requestStreamObserver = helloService.cs2s(new StreamObserver<HelloProto.HelloResponse>() {
            @Override
            public void onNext(HelloProto.HelloResponse value) {
                System.out.println(value.getResult());
            }

            @Override
            public void onError(Throwable t) {
                System.out.println("onError:" + t.getMessage());
            }

            @Override
            public void onCompleted() {
                System.out.println("onEonCompleted");
            }
        });
        for (int i = 0; i < 10; i++) {
            HelloProto.HelloRequest.Builder builder = HelloProto.HelloRequest.newBuilder();
            builder.setName("lwq" + i);
            HelloProto.HelloRequest request = builder.build();
            requestStreamObserver.onNext(request);
        }
        requestStreamObserver.onCompleted();
        managedChannel.shutdown();
        System.in.read();
    }

    @Test
    public void testCs2ss() throws IOException {
        ManagedChannel managedChannel = ManagedChannelBuilder.forAddress("127.0.0.1", 9999).usePlaintext().build();
        //Channel interceptChannel = ClientInterceptors.intercept(managedChannel, grpcTracing.newClientInterceptor());
        HelloServiceGrpc.HelloServiceStub helloService = HelloServiceGrpc.newStub(managedChannel);
        StreamObserver<HelloProto.HelloRequest> requestStreamObserver = helloService.cs2ss(new StreamObserver<HelloProto.HelloResponse>() {
            @Override
            public void onNext(HelloProto.HelloResponse value) {
                System.out.println(value.getResult());
            }

            @Override
            public void onError(Throwable t) {
                System.out.println("onError:" + t.getMessage());
            }

            @Override
            public void onCompleted() {
                System.out.println("onEonCompleted");
            }
        });
        for (int i = 0; i < 10; i++) {
            HelloProto.HelloRequest.Builder builder = HelloProto.HelloRequest.newBuilder();
            builder.setName("lwq" + i);
            HelloProto.HelloRequest request = builder.build();
            requestStreamObserver.onNext(request);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        requestStreamObserver.onCompleted();
        managedChannel.shutdown();

        System.in.read();
    }

}
